/*
 * Library.java
 *
 * Created on 30 de Agosto de 2007, 21:12
 *
 * @author Evolution Systems - Do the Evolution!!
 */

package util;

import java.sql.*;
import java.util.ArrayList;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import bd.Database;

public class Library 
{
    public static ArrayList getTables(Database bd) throws SQLException
    {
        ArrayList ls= new ArrayList();
        DatabaseMetaData md;
        ResultSet rs;
        Connection cn = bd.getConnection();

        String []type= {"TABLE", "VIEW"};		
        md= cn.getMetaData();

        rs = md.getTables(null,null,"%",type);
        
        while( rs.next())
            ls.add(rs.getString("TABLE_SCHEM")+"."+rs.getString("TABLE_NAME"));
        return ls;
    }

    public static ArrayList getFields(Database bd, String tableName) throws SQLException
    {
        ArrayList ls = new ArrayList();
        ResultSetMetaData md;
        ResultSet rs;
        int i=1;

        rs = bd.executeQueryRs("SELECT * FROM "+tableName+" LIMIT 1");
        md = rs.getMetaData();
        while (i <= md.getColumnCount())
            ls.add (md.getColumnName(i++));

        return ls;
    }

    public static ArrayList getTypesFields(Database bd, String tableName) throws SQLException
    {
        ArrayList ls = new ArrayList();
        ResultSetMetaData md;
        ResultSet rs;
        int i=1;

        rs = bd.executeQueryRs("SELECT * FROM "+tableName+" LIMIT 1");
        md = rs.getMetaData();
       
        while (i <= md.getColumnCount())
            ls.add (String.valueOf(md.getColumnTypeName(i++)));

        return ls;
    }
        
    public static ArrayList getLengthFields(Database bd, String tableName) throws SQLException
    {
        ArrayList ls = new ArrayList();
        ResultSetMetaData md;
        ResultSet rs;
        int i=1;

        rs = bd.executeQueryRs("SELECT * FROM "+tableName+" LIMIT 1");
        md = rs.getMetaData();
       
        while (i <= md.getColumnCount())
            ls.add (String.valueOf(md.getColumnDisplaySize(i++)));

        return ls;
    }

    public static int getNumberColumns(ResultSet rs) throws SQLException
    {
        ResultSetMetaData md = rs.getMetaData();
        return md.getColumnCount();
    }
    
    public static int getNumberRows(ResultSet rs) throws SQLException
    {
        int numberRows;
        
        rs.last();
        numberRows= rs.getRow();
        rs.beforeFirst();
        
        return numberRows;
    }
    
    public static boolean storeFile(String value, String path) 
    {

        try
        {
            File out = new File(path);
            FileWriter fw = new FileWriter(out);	
            PrintWriter pw = new PrintWriter(fw);

            pw.println(value);
            fw.close();
        }
        catch(IOException ee)
        {
            JOptionPane.showMessageDialog(null,"Excess�o! "+ee);
            return false;            
        }
        return true;
    }
    
    public static String[] convertToArrayOfString(ArrayList l)
    {
        String [] temp = new String[l.size()];
        
        l.toArray(temp);
        
        return temp;
    }
    
    public static String[] toStandardize(String[] types)
    {
       /*TODO: standardize all types of all databases hehehehhehehe*/
       
       for( int i=0 ; i < types.length; i++)
       {
         if( types[i].toString().toUpperCase().contains("INT"))
            types[i]= "int";
         else if( types[i].toString().toUpperCase().contains("DOUBLE"))
            types[i] = "double";
         else if( types[i].toString().toUpperCase().contains("FLOAT"))
            types[i] = "double";         
         else if( types[i].toString().toUpperCase().contains("BOOL"))
            types[i] = "boolean";
         else
            types[i] = "String";
       }
       return types;
    }
    
    public static JMenu makeMenu( String items[]){
        int tam, i;
        
        JMenu menu = new JMenu(items[0]);
        tam = items.length;
        
        for( i=1; i < tam; i++)
        {
            JMenuItem m = new JMenuItem( items[i] );
            //m.addActionListener(owner);
        }
        return menu;
    }    
}
